<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="活动" prop="activityId">
        <el-select v-model="queryParams.activityId" filterable placeholder="请选择活动" clearable>
          <el-option
            v-for="item in activityList"
            :key="item.activityId"
            :label="item.activityName"
            :value="item.activityId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建人" prop="createBy" v-hasRole="['admin']">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动类型" prop="activityType">
        <el-select v-model="queryParams.activityType" placeholder="请选择活动类型" clearable>
          <el-option
            v-for="dict in dict.type.event_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:enroll:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:enroll:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:enroll:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:enroll:export']"
        >
          导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="enrollList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="报名编号" align="center" prop="enrollId" />
      <el-table-column label="报名用户" align="center" prop="createBy" />
      <el-table-column label="活动名称" align="center" prop="apmActivity.activityName">
        <template slot-scope="scope">
          <div class="u-line-2" :title="scope.row.apmActivity.activityName">
            {{ scope.row.apmActivity.activityName }}
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="微信头像" align="center" prop="wxHead" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.wxHead" :width="50" :height="50" />
        </template>
      </el-table-column> -->
      <el-table-column label="活动开始时间" align="center" prop="apmActivity.startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.apmActivity.startTime, '{y}-{m}-{d} {h}:{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动结束时间" align="center" prop="apmActivity.endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.apmActivity.endTime, '{y}-{m}-{d} {h}:{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动类型" align="center" prop="apmActivity.activityType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.event_type" :value="scope.row.apmActivity.activityType" />
        </template>
      </el-table-column>
      <el-table-column label="是否到场" align="center" prop="persentStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.activity_participation_status"
            :value="scope.row.persentStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleUpdate(scope.row)">
            查看
          </el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:enroll:remove']"
          >
            删除
          </el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      close-on-press-escape
    >
      <div class="test-form">
        <el-form
          ref="userData"
          :model="form"
          label-position="left"
          label-width="80px"
          :rules="rules"
          v-if="form.apmActivity"
        >
          <el-form-item label="活动名称" prop="apmActivity.activityName">
            <el-input
              v-model="form.apmActivity.activityName"
              placeholder="请输入审批备注"
              disabled
            />
          </el-form-item>
          <el-form-item label="活动类型" prop="apmActivity.activityName">
            <el-select
              v-model="form.apmActivity.activityType"
              placeholder="请选择活动类型"
              disabled
            >
              <el-option
                v-for="dict in dict.type.event_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <el-divider content-position="left">用户提交内容</el-divider>
        <parser :key="new Date().getTime()" :form-conf="formConfig" />
      </div>

      <div slot="footer" class="dialog-footer">
        <template v-if="form.isPass == 0">
          <el-button type="primary" @click="approvalT(1)">通 过</el-button>
          <el-button @click="approvalT(2)">驳回</el-button>
        </template>
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改活动报名登记对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body close-on-press-escape>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="活动id" prop="activityId">
          <el-input v-model="form.activityId" placeholder="请输入活动id" />
        </el-form-item>
        <el-form-item label="微信头像" prop="wxHead">
          <image-upload v-model="form.wxHead"/>
        </el-form-item>
        <el-form-item label="是否通过" prop="isPass">
          <el-select v-model="form.isPass" placeholder="请选择是否通过">
            <el-option
              v-for="dict in dict.type.approval_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import {
  listEnroll,
  getEnroll,
  delEnroll,
  addEnroll,
  updateEnroll,
  approveEnroll
} from '@/api/apm/enroll'
// import formConfig from '@/json/file.json';
import Parser from '@/components/parser/Parser.vue'
import { listActivity } from '@/api/apm/activity'
export default {
  name: 'Enroll',
  dicts: ['approval_status', 'event_type'],
  components: { Parser },
  data() {
    return {
      formTitle: '',
      formConfOpen: false,
      activeName: 1,
      userData: { advice: '' },
      formData: {}, // 用于存储表单数据
      formConfig: null, // 导入的表单配置信息
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      activityList: [],
      // 活动报名登记表格数据
      enrollList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isPass: 1
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        advice: [{ required: true, message: '请输入审批备注', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.queryParams.createBy = this.$store.getters.name
    this.getList()
    this.getActivityList()
  },
  methods: {
    /** 查询活动列表 */
    getActivityList() {
      listActivity({ pageNum: 1, pageSize: 100, createBy: this.$store.getters.name }).then(
        (response) => {
          this.activityList = response.rows
          this.loading = false
        }
      )
    },
    handleSubmit() {
      // 处理表单提交逻辑
    },
    /** 查询活动报名登记列表 */
    getList() {
      this.loading = true
      listEnroll(this.queryParams).then((response) => {
        this.enrollList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        enrollId: null,
        activityId: null,
        wxHead: null,
        isPass: null,
        createBy: this.$store.getters.name,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        advice: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.enrollId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加活动报名登记'
    },
    /** 修改按钮操作 */
    // handleUpdate(row) {
    //   this.reset();
    //   const enrollId = row.enrollId || this.ids
    //   getEnroll(enrollId).then(response => {
    //     this.form = response.data;
    //     let dataJson = JSON.parse(response.data.information)
    //     this.formConfig=dataJson
    //     this.open = true;
    //     this.title = "修改活动报名登记";
    //   });
    // },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const enrollId = row.enrollId || this.ids
      getEnroll(enrollId).then((response) => {
        this.form = response.data
        let dataJson = JSON.parse(response.data.information)
        dataJson.disabled = true
        dataJson.formBtns = false
        dataJson.labelWidth = 80
        this.formConfig = dataJson
        this.open = true
        this.title = '活动报名登记'
      })
    },
    approvalT(isPass) {
      const form = this.form
      this.$refs['userData'].validate((valid) => {
        if (valid) {
          approveEnroll({ enrollId: form.enrollId, isPass, advice: form.advice }).then(
            (response) => {
              this.$modal.msgSuccess('操作成功')
              this.open = false
              this.getList()
            }
          )
        }
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.enrollId != null) {
            updateEnroll(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addEnroll(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const enrollIds = row.enrollId || this.ids
      this.$modal
        .confirm('是否确认删除活动报名登记编号为"' + enrollIds + '"的数据项？')
        .then(function () {
          return delEnroll(enrollIds)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'apm/enroll/export',
        {
          ...this.queryParams
        },
        `enroll_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
